home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / dev / c / ExtrasLib.lha / ExtrasLib / Source / StrFontLen.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-30  |  6.3 KB  |  284 lines

  1. #include <extras/math.h>
  2. #define __USE_SYSBASE
  3. #include <strings.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <graphics/text.h>
  7. #include <clib/extras_protos.h>
  8.  
  9. #include <extras/ext_text.h>
  10. #include <string.h>
  11. #include <tagitemmacros.h>
  12. #include <math.h>
  13.  
  14. #include <proto/graphics.h>
  15. #include <proto/utility.h>
  16.  
  17. #define DEBUG
  18. #include <debug.h>
  19.  
  20. /****** extras.lib/gui_StrFontLen ******************************************
  21. *
  22. *   NAME
  23. *       gui_StrFontLen - get the pixel length of a string.
  24. *
  25. *   SYNOPSIS
  26. *       length=gui_StrFontLen(Font, Str)
  27. *
  28. *       LONG gui_StrFontLen(struct TextFont *,STRPTR);
  29. *
  30. *   FUNCTION
  31. *       This function returns the number of pixels a string
  32. *       would occupy if rendered in the specified Font.
  33. *
  34. *   INPUTS
  35. *       Font - struct TextFont * previously opened by 
  36. *              OpenFont() or OpenDiskFont().
  37. *       Str - a pointer to a null terminated string.
  38. *
  39. *   RESULT
  40. *       the pixel length of the string or 0 if either parameter
  41. *       is NULL.
  42. *
  43. ******************************************************************************
  44. *
  45. */
  46.  
  47. LONG gui_StrFontLen (struct TextFont *Font, STRPTR Str)
  48. {
  49.   return(gui_StrLength(SL_TextFont ,Font,
  50.             SL_String   ,Str,
  51.             TAG_DONE));
  52. }
  53.  
  54.  
  55. /****** extras.lib/gui_StrLength ******************************************
  56. *
  57. *   NAME
  58. *       gui_StrLength -- Get the pixel length of a string.
  59. *
  60. *   SYNOPSIS
  61. *       Len = gui_StrLength(Tags)
  62. *       
  63. *       LONG gui_StrLength(Tag, ... );
  64. *
  65. *   FUNCTION
  66. *       Find the length of a given string.  If multiple strings
  67. *       are given, returns the longest length.
  68. *
  69. *   INPUTS
  70. *       Tags 
  71. *         SL_TextFont - (struct TextFont *)The font the string will
  72. *           be rendered in. (required)
  73. *         SL_IgnoreChars - (STRPTR) Null terminated string of characters
  74. *           to ignore from the length calculation, useful for underscores
  75. *           in gadget text, etc.
  76. *         SL_String - (STRPTR) String to size.
  77. *
  78. *   RESULT
  79. *       Length in pixels of longest string.
  80. *
  81. *   EXAMPLE
  82. *         *find length of button text without the "_" 
  83. *         len= gui_StrLength(SL_TextFont     ,TF,
  84. *                        SL_String       ,"_Button",
  85. *                        SL_IgnoreChars  ,"_",
  86. *                        TAG_DONE);
  87. *
  88. *         * find maximum length of red, green and blue without "_"
  89. *         len= gui_StrLength(SL_TextFont     ,TF,
  90. *                        SL_String       ,"_Red",
  91. *                        SL_String       ,"_Green",
  92. *                        SL_String       ,"_Blue",
  93. *                        SL_IgnoreChars  ,"_",
  94. *                        TAG_DONE);
  95. *
  96. *   NOTES
  97. *
  98. *   BUGS
  99. *
  100. *   SEE ALSO
  101. *
  102. ******************************************************************************
  103. *
  104. */
  105.  
  106. LONG gui_StrLength (Tag Tags, ... )
  107. {
  108.   struct RastPort rp;
  109.   STRPTR str,ignore,cstr;
  110.   struct TextFont   *tf;
  111.   struct TextExtent tex;
  112.   struct TagItem *taglist,*tag,*tstate;
  113.   LONG l,l2=0,c;
  114.  
  115. //  DKP("StrLength GfxBase=%8lx  UtilityBase=%8lx\n",GfxBase,UtilityBase);
  116.     
  117.   taglist=(struct TagItem *)&Tags;
  118.   
  119.   tf=(struct TextFont *)GetTagData(SL_TextFont,0,taglist);
  120.   ignore=(STRPTR)GetTagData(SL_IgnoreChars,0,taglist);
  121.   
  122.   
  123.   if(!tf)
  124.     return(0);
  125.  
  126.  
  127.   InitRastPort(&rp);
  128.   SetFont(&rp,tf);
  129.  
  130.   ProcessTagList(taglist,tag,tstate)
  131.   {
  132.     if(tag->ti_Tag==SL_String)
  133.     {
  134.       l=0;
  135.       if(str=(STRPTR)tag->ti_Data)
  136.       {
  137.         cstr=0;
  138.         if(ignore)
  139.         {
  140.           if(cstr=AllocVec(strlen(str)+1,0))
  141.           { // Copy str to cstr, skipping chars in "ignore"
  142.             c=0;
  143.             while(*str)
  144.             {
  145.               if(!strchr(ignore,*str))
  146.               {
  147.                 cstr[c]=*str;
  148.                 c++;
  149.               }
  150.               str++;
  151.             }
  152.             cstr[c]=0;
  153.             str=cstr;
  154.           }
  155.         }
  156. //        l=TextLength(&rp,str,strlen(str));
  157. //        DKP("String %s\n",str);
  158.         TextExtent(&rp,str,strlen(str),&tex);
  159.  
  160.         l=tex.te_Extent.MaxX-tex.te_Extent.MinX+1;        
  161.         
  162. //        printf("%40.40s  tex.Width:%7d tex.MinX:%7d tex.MaxX:%7d  l:%7d\n",str,tex.te_Width,tex.te_Extent.MinX,tex.te_Extent.MaxX,l);
  163.         FreeVec(cstr);  // Safe to call with NULL
  164.         
  165.       }
  166.       l2=max(l,l2);
  167.     }
  168.   }
  169.   return(l2);
  170. }
  171.  
  172. /*
  173. LONG StrLength (Tag Tags, ... )
  174. {
  175.   struct RastPort rp;
  176.   STRPTR str,ignore;
  177.   struct TextFont *tf;
  178.   struct TagItem *taglist,*tag,*tstate;
  179.   LONG l,l2=0,c,ilen=0,kset=0,kern=0,firstkern=0;
  180.   UBYTE s;
  181.     
  182.   taglist=(struct TagItem *)&Tags;
  183.   
  184.   tf=(struct TextFont *)GetTagData(SL_TextFont,0,taglist);
  185.   if(ignore=(STRPTR)GetTagData(SL_IgnoreChars,0,taglist))
  186.     ilen=strlen(ignore);
  187.   
  188.   
  189.   if(!tf)
  190.     return(0);
  191.  
  192.  
  193.   InitRastPort(&rp);
  194.   SetFont(&rp,tf);
  195.  
  196.   ProcessTagList(taglist,tag,tstate)
  197.   {
  198.     if(tag->ti_Tag==SL_String)
  199.     {
  200.       l=0;
  201.       if(str=(STRPTR)tag->ti_Data)
  202.       {
  203.         if(tf->tf_Flags & FPF_PROPORTIONAL)
  204.         {
  205.           UBYTE s,lo,hi,add;
  206.           lo=tf->tf_LoChar;
  207.           hi=tf->tf_HiChar;
  208.           
  209.           while(*str)
  210.           {
  211.             add=TRUE;
  212.             s=*str;
  213.             
  214.             if(s>=lo && s<=hi)
  215.               s-=lo;
  216.             else
  217.               s=hi-lo+1;
  218.               
  219.             for(c=0;c<ilen;c++)
  220.             {
  221.               if(s==ignore[c]) 
  222.                 add=FALSE;
  223.             }
  224.             
  225.             
  226.             
  227.             if(add)
  228.             {
  229. //        kprintf("%lc - Added\n",s);
  230.               kern=((WORD *)tf->tf_CharKern)[s];
  231.               l+=((WORD *)tf->tf_CharSpace)[s]+ kern ;
  232.               if(!kset)
  233.               {
  234.                 kset=1;
  235.                 firstkern=kern;
  236.               }
  237.             }
  238.             else
  239.             {
  240. //        kprintf("%lc - Ignored\n",s);
  241.             }
  242.             str++;
  243.           }
  244.           
  245.           l=l-firstkern;
  246.         }
  247.         else
  248.         {
  249.           UBYTE c,add;
  250.           WORD xsize;
  251.           
  252.           xsize=tf->tf_XSize;
  253.           while(*str)
  254.           {
  255.             s=*str;
  256.             add=TRUE;
  257.       
  258.             for(c=0;c<ilen;c++)
  259.             {
  260.               if(s==ignore[c]) 
  261.                 add=FALSE;
  262.             }
  263.             
  264.             if(add)
  265.             {
  266. //        kprintf("%lc - Added\n",s);
  267.               l+=xsize;
  268.             }
  269.             else
  270.             {
  271. //        kprintf("%lc - Ignored\n",s);
  272.             } 
  273.             str++;
  274.           }
  275.         } 
  276.       }
  277.       l=abs(l);
  278.       l2=max(l,l2);
  279.     }
  280.   }
  281.   return(l2);
  282. }
  283.  
  284. */